﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.Data;
using System.Collections;

public partial class Controls_Message_SystemMessage : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Message> sysmsglist = MessageRules.Instance.GetCurrentUserSystemMessages();
        this.lt_MessageCount.Text = sysmsglist.Count.ToString();
        if (sysmsglist.Count > 0)
        {
            this.rpt_SystemMessage.DataSource = sysmsglist;
            this.rpt_SystemMessage.DataBind();
            MessageRules.Instance.SignReadSystemMessage();
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
             
    }



    protected void LoadControl(Panel pnSystemMessage,Message message)
    {
        string[] param = GetParameters(message);
        string vitualUrl = GetAppControlUrl(param[0], param[1]);
        ISystemMessageControl smControl = (ISystemMessageControl)LoadControl(vitualUrl);
        smControl.Initialize(message.MessageId,param);
        Control curSmControl = smControl as Control;
        pnSystemMessage.Controls.Add(curSmControl);
    }

    private string GetAppControlUrl(string systemName, string requestType)
    {
        return  "/Controls/SystemMessage/" + systemName + "/"  + requestType + ".ascx";                
    }

    

    private string[] GetParameters(Message message)
    {
        string[] parameters;
        parameters=message.MessageContent.Split(new string[] { "#####" }, StringSplitOptions.None);
        ArrayList paramlist = new ArrayList();
        foreach (string singleparam in parameters)
        {
            paramlist.Add(singleparam);
        }
        
        //paramlist.Add(message.MessageId.ToString());
        string[] finalparam = (string[])paramlist.ToArray(typeof(string));
        
        return finalparam;

        
    }

    protected void lbt_ClearMessage_Click(object sender, EventArgs e)
    {
        MessageRules.Instance.DeleteCurrentUserSystemMessage();
        this.pn_Delete.Visible = true;
        List<Message> sysmsglist = MessageRules.Instance.GetCurrentUserSystemMessages();
        this.lt_MessageCount.Text = sysmsglist.Count.ToString();
        this.rpt_SystemMessage.DataSource = sysmsglist;
        this.rpt_SystemMessage.DataBind();
        
        
    }

    protected void rpt_SystemMessage_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string messageId = e.CommandArgument.ToString();
            MessageRules.Instance.DeleteMessageByMessageId(messageId.ToString());
            this.pn_Delete.Visible = true;
            this.lt_MessageCount.Text = MessageRules.Instance.GetCurrentUserSystemMessages().Count.ToString();
            this.rpt_SystemMessage.DataSource = MessageRules.Instance.GetCurrentUserSystemMessages();
            this.rpt_SystemMessage.DataBind();
        }
    }
    protected void rpt_SystemMessage_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Message message = e.Item.DataItem as Message;
        Literal temp_ltSendDate = e.Item.FindControl("lt_SendDate") as Literal;
        Literal temp_ltSendTime = e.Item.FindControl("lt_SendTime") as Literal;
        temp_ltSendDate.Text = message.SendDateTime.ToShortDateString();
        temp_ltSendTime.Text = message.SendDateTime.ToShortTimeString();
        LinkButton temp_lbtClose = e.Item.FindControl("lbt_Close") as LinkButton;
        temp_lbtClose.CommandArgument = message.MessageId.ToString();
        Panel temp_pnSysmsg = e.Item.FindControl("pn_SystemMessage") as Panel;
        LoadControl(temp_pnSysmsg, message);
        
    }
    protected void rpt_SystemMessage_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            List<Message> sysmsglist = MessageRules.Instance.GetCurrentUserSystemMessages();
            this.lt_MessageCount.Text = sysmsglist.Count.ToString();
            if (sysmsglist.Count > 0)
            {
                this.rpt_SystemMessage.DataSource = sysmsglist;
                this.rpt_SystemMessage.DataBind();
            }
        }
    }
    protected void rpt_SystemMessage_PreRender(object sender, EventArgs e)
    {
        List<Message> sysmsglist = MessageRules.Instance.GetCurrentUserSystemMessages();
        this.lt_MessageCount.Text = sysmsglist.Count.ToString();
        if (sysmsglist.Count > 0)
        {
            this.rpt_SystemMessage.DataSource = sysmsglist;
            this.rpt_SystemMessage.DataBind();
        }
    }
}
